﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace BDSim
{
    /// <summary>
    /// 读取行星历表动态库封装
    /// </summary>
    public class ReadEphdat
    {
        /// <summary>
        /// 读行星历表初始化函数
        /// </summary>
        /// <param name="fileName">文件的全路劲名称</param>
        /// <param name="startTjd">起始时间（儒略历形式）</param>
        /// <param name="endTjd">结束时间（儒略历形式）</param>
        /// <returns>执行正确返回ture，否则false</returns>
        [DllImport("ReadEphdat.dll", EntryPoint = "RE_Init", CallingConvention = CallingConvention.Cdecl)]
        private static extern bool RE_Init(char[] fileName, double startTjd, double endTjd);
        //封装为年月日的接口形式
        public static bool RE_Init(char[] fileName, UTCTime start, UTCTime end)
        {
            BDSim.TimeSpaceDll.CAL2UTC(out start, 1);
            BDSim.TimeSpaceDll.CAL2UTC(out end, 1);
            return RE_Init(fileName, start.JD, end.JD);
        }

        /// <summary>
        /// 获取行星历表数据
        /// </summary>
        /// <param name="row">row 数据行数</param>
        /// <param name="col">col 数据列数（默认1018，即每行有1018个数据）</param>
        /// <returns>行星历表数据首地址addr,一位数组，第i行第j列的数据位addr[i*col+j]</returns>
        [DllImport("ReadEphdat.dll", EntryPoint = "RE_GetEphdat", CallingConvention = CallingConvention.Cdecl)]
        unsafe public static extern double* RE_GetEphdat(out int row, out int col);

        /// <summary>
        /// 获取EphData文件信息
        /// </summary>
        /// <param name="filename">EphDat全路劲文件名</param>
        /// <param name="info">文件信息</param>
        /// <returns>1:成功; 负数和0:失败</returns>
        [DllImport("ReadEphdat.dll", EntryPoint = "RE_GetEphDatInfo", CallingConvention = CallingConvention.Cdecl)]
        private static extern int re_getEphDatInfo(char[] filename, out EphDatFileInfo info);
        //封装为EOP文件的接口形式
        public static int RE_GetEphDatInfo(char[] filename, out EopFileInfo info)
        {
            EphDatFileInfo fileinfo = new EphDatFileInfo();
            UTCTime start = new UTCTime();
            UTCTime end = new UTCTime();

            int ret = re_getEphDatInfo(filename, out fileinfo);
            start.JD = fileinfo.startTime;
            end.JD = fileinfo.endTime;

            BDSim.TimeSpaceDll.CAL2UTC(out start, 2);
            BDSim.TimeSpaceDll.CAL2UTC(out end, 2);

            info.startTime = start.ToString().ToCharArray();
            info.endTime = end.ToString().ToCharArray();
            info.lineNum = fileinfo.lineNum;

            return ret;
        }
    }
}
